/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.commons.util;



import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.imageio.ImageIO;

import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

/**
 * 说明：条形码生成
 * 
 * @Author：李智深
 * @Date：2012-03-29
 */
public class SampleBitmapBarcodeWithBean {

	/**
	 * 输入条形码编号，返回生成图片路径
	 * */
    public static Image createCode39Bean(String number){
    	 File outputFile=null;
    	try {
    		Code39Bean bean=new Code39Bean();
            final int dpi = 100;
            bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
//            bean.setModuleWidth(0.2); //makes the narrow bar
            bean.setHeight(15);
//            bean.setWideFactor(3);
            bean.doQuietZone(false);
           
            outputFile = new File(number+".jpg");
            ByteArrayOutputStream   out = new ByteArrayOutputStream ();
//            OutputStream   out = new FileOutputStream (outputFile);
            try {
                BitmapCanvasProvider canvas = new BitmapCanvasProvider( out, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
                bean.generateBarcode(canvas, "123456");
                canvas.finish();
            } finally {
                out.close();
            }
            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            return ImageIO.read(in); 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
